package com.starhub.monitor.controller;

import com.starhub.common.bean.ResultResp;
import com.starhub.monitor.dto.ActivityLogDto;
import com.starhub.monitor.dto.AgentUsageStatsDto;
import com.starhub.monitor.dto.MonitorStatsDto;
import com.starhub.monitor.dto.SystemResourcesDto;
import com.starhub.monitor.dto.ChatLogDetailDto;
import com.starhub.monitor.service.IMonitorStatsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 监控报表控制器
 */
@RestController
@RequestMapping("/engine/starhub/monitor")
@Api(tags = "监控统计接口")
public class MonitorController {
    
    @Autowired
    private IMonitorStatsService monitorStatsService;
    
    @GetMapping("/today-requests")
    @ApiOperation("获取今日总请求次数")
    public ResultResp<Long> getTodayTotalRequests() {
        return ResultResp.success(monitorStatsService.getTodayTotalRequests());
    }
    
    @GetMapping("/total-agents")
    @ApiOperation("获取智能体总数")
    public ResultResp<Long> getTotalAgents() {
        return ResultResp.success(monitorStatsService.getTotalAgents());
    }
    
    @GetMapping("/total-users")
    @ApiOperation("获取总用户数")
    public ResultResp<Long> getTotalUsers() {
        return ResultResp.success(monitorStatsService.getTotalUsers());
    }
    
    @GetMapping("/total-api-calls")
    @ApiOperation("获取API调用次数")
    public ResultResp<Long> getTotalApiCalls() {
        return ResultResp.success(monitorStatsService.getTotalApiCalls());
    }
    
    @GetMapping("/total-conversations")
    @ApiOperation("获取对话总数")
    public ResultResp<Long> getTotalConversations() {
        return ResultResp.success(monitorStatsService.getTotalConversations());
    }
    
    @GetMapping("/total-alerts")
    @ApiOperation("获取告警数量")
    public ResultResp<Long> getTotalAlerts() {
        return ResultResp.success(monitorStatsService.getTotalAlerts());
    }
    
    @GetMapping("/total-errors")
    @ApiOperation("获取错误数量")
    public ResultResp<Long> getTotalErrors() {
        return ResultResp.success(monitorStatsService.getTotalErrors());
    }
    
    @GetMapping("/total-normals")
    @ApiOperation("获取正常数量")
    public ResultResp<Long> getTotalNormals() {
        return ResultResp.success(monitorStatsService.getTotalNormals());
    }
    
    @GetMapping("/activity-logs")
    @ApiOperation("获取系统活动日志")
    public ResultResp<List<ActivityLogDto>> getActivityLogs(
            @ApiParam(value = "限制返回记录数", defaultValue = "20")
            @RequestParam(value = "limit", defaultValue = "20") Integer limit) {
        return ResultResp.success(monitorStatsService.getRecentActivityLogs(limit));
    }
    
    @GetMapping("/time-stats")
    @ApiOperation("获取按时间段统计的请求数据（包含正常数、错误数和告警数）")
    public ResultResp<MonitorStatsDto> getRequestStatsByTime(
            @ApiParam(value = "统计类型: day-按日统计，week-按周统计，month-按月统计", defaultValue = "day")
            @RequestParam(value = "type", defaultValue = "day") String type,
            
            @ApiParam(value = "统计时间: day-具体日期(2024-03-20)，week-月份(2024-03)，month-年份(2024)", required = true)
            @RequestParam(value = "time") String time) {
        return ResultResp.success(monitorStatsService.getRequestStatsByTime(type, time));
    }
    
    @GetMapping("/dashboard")
    @ApiOperation("获取仪表盘所有统计数据")
    public ResultResp<MonitorStatsDto> getDashboardStats() {
        return ResultResp.success(monitorStatsService.getDashboardStats());
    }
    
    @GetMapping("/custom-time-stats")
    @ApiOperation("根据自定义时间范围获取请求统计数据（包含正常数、错误数和告警数）")
    public ResultResp<MonitorStatsDto> getRequestStatsByCustomTimeRange(
            @ApiParam(value = "开始时间", required = true)
            @RequestParam(value = "startTime") 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            
            @ApiParam(value = "结束时间", required = true)
            @RequestParam(value = "endTime") 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            
            @ApiParam(value = "时间间隔类型: hour-按小时，day-按天，month-按月", defaultValue = "day")
            @RequestParam(value = "interval", defaultValue = "day") String interval) {
        
        return ResultResp.success(monitorStatsService.getRequestStatsByCustomTimeRange(
                startTime, endTime, interval));
    }
    
    @GetMapping("/system-resources")
    @ApiOperation("获取系统资源使用情况")
    public ResultResp<SystemResourcesDto> getSystemResources() {
        SystemResourcesDto resources = monitorStatsService.getSystemResources();
        return ResultResp.success(resources);
    }
    
    @GetMapping("/agent-usage-stats")
    @ApiOperation("获取智能体使用统计数据")
    public ResultResp<List<AgentUsageStatsDto>> getAgentUsageStats() {
        return ResultResp.success(monitorStatsService.getAgentUsageStats());
    }
    
    @GetMapping("/chat-logs")
    @ApiOperation("获取智能体调用日志记录详情")
    public ResultResp<List<ChatLogDetailDto>> getChatLogDetails(
            @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("状态：0-失败，1-成功，2-告警") @RequestParam(required = false) Integer status) {
        return ResultResp.success(monitorStatsService.getChatLogDetails(startTime, endTime, status));
    }
} 